// 2025/9/23
// 数组中两个字符串的最小距离

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n; cin >> n;
    string str1, str2, str;
    cin >> str1 >> str2;
    
    int pos1 = -1, pos2 = -1, ans = n;
    for(int i = 0; i < n; i++)
    {
        cin >> str;
        if(str == str1)
        {
            if(pos2 != -1)
                ans = min(ans, i - pos2);
            pos1 = i;
        }
        else if(str == str2)
        {
            if(pos1 != -1)
                ans = min(ans, i - pos1);
            pos2 = i;
        }
    }
    if(pos1 == -1 || pos2 == -1)
        cout << -1 << endl;
    else
        cout << ans << endl;
}
// 64 位输出请用 printf("%lld")